<template>
  <el-form ref="deptForm" :model="form" label-width="120px" :rules="rules">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option
          v-for="item in employeesList"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { addDepartments, getDepartmentsDetail, editDepartmentsDetail } from '@/api/departments'

import { getEmployeesSimple } from '@/api/emplogyees'
export default {
  props: {
    id: {
      required: true,
      type: String
    },
    isEdit: {
      required: true,
      type: Boolean
    },
    originList: {
      required: true,
      type: Array
    }
  },
  data() {
    // 校验部门的编码唯一性
    const validCode = (rule, value, callback) => {
      let existCodeList = this.originList.map(item => item.code)
      if (this.isEdit) {
        // existCodeList = this.originList.filter(item => item.id !== this.id).map(item => item.code)
        existCodeList = existCodeList.filter(item => item.code !== this.code)
      }
      if (existCodeList.includes(value)) {
        // 存在，抛出错误，提示用户
        callback(new Error(`重复的code${value},请重新输入`))
      } else {
        callback()
      }
    }
    // 新增时校验同级不能出出现重复
    const validName = (rule, value, callback) => {
      let existNameList = this.originList.filter(item => item.pid === this.id).map(item => item.name)
      if (this.isEdit) {
        const pid = this.originList.find(item => item.id === this.id).pid
        const brothers = this.originList.filter(item => item.pid === pid).filter(item => item.id !== this.id)
        existNameList = brothers.map(item => item.name)
        if (existNameList.includes(value)) {
          callback(new Error(`重复的${value},请重新输入`))
        } else {
          callback()
        }
      }
    }
    return {
      rules: {
        name: [
          { required: true, message: '请输入部门名称', tigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50字内容', tigger: 'blur' },
          { validator: validName, tigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', tigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50字内容', tigger: 'blur' },
          { validator: validCode, tigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请选择部门负责人', tigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', tigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50字内容', tigger: 'blur' }
        ]
      },

      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      employeesList: []
    }
  },
  async created() {
    this.LoadingEmployeesSimple()
    if (this.isEdit) { this.LoadingDepartmentsDetail() }
  },
  methods: {
    // 重置表单规则
    resetForm() {
      this.$refs.deptForm.resetFields()
    },
    // 确定
    hSubmit() {
      // 完成兜底校验
      this.$refs.deptForm.validate(valid => {
        if (!valid) return // 校验失败
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    // 取消
    hCancel() {
      this.$emit('close')
    },
    async doEdit() {
      // console.log(1111)
      try {
        const res = await editDepartmentsDetail(this.form)
        if (res.code === 10000) {
          this.$message.success(res.message)
          this.$emit('success')
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 添加
    async  doAdd() {
      try {
        const res = await addDepartments({ ...this.form, pid: this.id })
        if (res.code === 10000) {
          this.$emit('success')
          this.$message.success(res.message)
        }
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    },
    async LoadingEmployeesSimple() {
      try {
        const res = await getEmployeesSimple()
        // console.log(res)
        if (res.code === 10000) {
          this.employeesList = res.data
        }
        // console.log(res)
      } catch (error) {
        console.log(error)
      }
    },
    async LoadingDepartmentsDetail() {
      try {
        const res = await getDepartmentsDetail(this.id)
        // console.log(res)
        this.form = res.data
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
